import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import {  queryUserInfo } from "@/api/layout";
import { Login } from "@/types/login";


/**
 * 异步获取热门文章列表
 */
export const getUserInfo = createAsyncThunk(
  "permission/getUserInfo",
  async (): Promise<Login.UserInfo> => {
    const res = await queryUserInfo();
    if (res.code === 200) {
    
      return {
        user:res.data ? res.data.user : {},
        roles:res.data.roles||[],
        permissions:res.data ? res.data.permissions : [],
      }
     
    }
    return {
      user: {},
      permissions: [],
      roles: []
    };
  }
);



/**
 * 菜单
 */
export const permissionSlice = createSlice({
  name: "permission",
  initialState: {
    userInfo: <Login.UserInfo>{
      user: {},
      permissions: [],
      roles: []
    },
  },
  reducers: {
   
  },
  extraReducers(builder) {
    // 当获取热门文章列表成功时，更新状态
    builder
    .addCase(getUserInfo.fulfilled, (state, action) => {
      state.userInfo = action.payload;
    });
 
  },
});

export default permissionSlice.reducer;